using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class AlphabetGenerator : MonoBehaviour {

    public GameObject Source;
    public GameObject Target;
    public GameObject Target2;
    public int count = 6;
    public float interval = 0.1f;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    private void ClearChildren(GameObject t)
    {
        for(int i=0; i<t.transform.GetChildCount(); i++)
        {
            GameObject child = t.transform.GetChild(i).gameObject;
            GameObject.Destroy(child);
        }
    }

    public void Gen()
    {
        List<GameObject> list = new List<GameObject>();
        Transform t = Source.transform;
        for (int i = 0; i < t.GetChildCount(); i++)
        {
            list.Add(t.GetChild(i).gameObject);
        }
        ClearChildren(Target);
        ClearChildren(Target2);
        remain.Clear();
        for (int i = 0; i < count; i++)
        {
            int index = Random.Range(0, list.Count);
            GameObject go = GameObject.Instantiate(list[index]) as GameObject;
            go.transform.parent = Target.transform;

            go.transform.localPosition = new Vector3(interval * i, 0, -1);
            remain.Add(go);
            list.RemoveAt(index);
        }
    }

    public int RemainCount
    {
        get
        {
            //Debug.Log("RemainCount:"+remain.Count);
            return remain.Count;
        }
    }

    public GameObject CurrLetter
    {
        get
        {
            if (remain.Count > 0)
                return remain[0];
            return null;
        }
    }

    public static List<GameObject> remain = new List<GameObject>();
}
